/*
  Part of the MasterTool3D project - http://code.google.com/p/master-tool-3d

  Copyright (c) 2011 Ingo Pueschl

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation, version 2.1.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
*/

package techjit.gfx.core;

import processing.core.PApplet;
import techjit.gfx.plugins.swe.SWE_Element;
import techjit.gfx.plugins.swe.drawing.TextDrawer;
import techjit.gfx.util.PU;
import techjit.gfx.util.geo.Dimensions;
import techjit.gfx.util.geo.Vector;
import techjit.util.LogManager;
import techjit.util.RuntimeParams;

public class GFX_Label extends GFX_Node {

	// FIELDS
	// head
	private int labelId;
	private static int nextLabelId = RuntimeParams.GFX__FIRST_ID;

	// CONSTRUCTORS
	public GFX_Label(GFX_Scene rootScene) {
		// head
		super(rootScene);
		labelId = nextLabelId++;

		// body
		changeLocalDimensions(new Dimensions(2, 1, 1));

		// tail
		logConstructorResult();
	}

	// METHODS
	@Override
	protected void arrange() {
		// head
		logArrangementInvocation();

		// body
		super.arrange();

		// tail
		logLocalArrangementResult();
	}

	@Override
	public void changeLocalDimensions(Dimensions dimensions) {
		super.changeLocalDimensions(dimensions);
	}

	@Override
	public void draw(PApplet p) {
		p.pushMatrix();
		p.pushStyle();
		{
			// head
			super.draw(p);


			// <r522> Just a 2x1x1 gray box without stroke.
//				// body
//				p.fill(RuntimeParams.DRAWING__FILL_COLOR__LABELS);
//				p.noStroke();
//
//				PU.box(p, getLocalPosition(new Vector(0)),
//						getLocalPosition(new Vector(1)));
//
//				// getNode().getName();
			// </r522> ==> <r522> Copied Elenas code from GFX_Label_EJR.java here.
//				// body
//				p.fill(RuntimeParams.DRAWING__FILL_COLOR__LABELS);
//				p.stroke(2);//.noStroke();
//
//				Vector v111 = getLocalPosition(new Vector(1));
//				Vector v000 = getLocalPosition(new Vector(0));
//				PU.box(p, v000, v111);
//				v111.subtract(v000);
//				String name = "NameHalloHalloallo";//alloHalloNameHallo
//				TextDrawer.text(p,
//						new Vector(v111.getX(), v111.getY() * 20, v111.getZ()),
//						name, RuntimeParams.COLOR__WHITE, 45);
//				float nameWidth = p.textWidth(name);
//				changeLocalDimensions(new Dimensions(nameWidth / 155, v111.getY(),
//						v111.getZ()));
//				break;
			// </r522> ==> <r522> Changed 155 to 155 / 5 = 31.
				// body
			{
				p.fill(RuntimeParams.DRAWING__FILL_COLOR__LABELS);
				p.noStroke();

				Vector v000 = getLocalPosition(new Vector(0));
				Vector v001 = getLocalPosition(new Vector(0,0,.25f));
				Vector v010 = getLocalPosition(new Vector(0,1,0));
				Vector v100 = getLocalPosition(new Vector(1,0,0));
//				PU.box(p, v000, v111);

				// x adjacent
				PU.trapezoid(p, v000, v010, v001);
				
				// x remote
				PU.trapezoid(p, v100, v010, v001);
				
				// y adjacent
				PU.trapezoid(p, v000, v100, v001);
				
				// y remote
				PU.trapezoid(p, v010, v100, v001);
				
				// z remote
				PU.trapezoid(p, v001, v100, v010);
				
				
				// bottom
//				PU.trapezoid(p, v000, v100, v001);
//				// left
//				PU.rectangle(p, v000, v011);
//				// back
//				PU.rectangle(p, v001, v111);
//				// top
//				PU.rectangle(p, v010, v111);
//				// right
//				PU.rectangle(p, v100, v111);
				// 
				
			}

			
			{
			Vector v000 = getLocalPosition(new Vector(0));
			Vector v111 = getLocalPosition(new Vector(1));
				
				v111.subtract(v000);
				// <r529>
//				String name = "NameHalloHalloallo";//alloHalloNameHallo
				// </r529> ==> <r529>
				GFX_LineLayout parentLineLayout = (GFX_LineLayout)navi_getParentObject();
				SWE_Element sweElement = (SWE_Element)parentLineLayout.navi_getFirstChild();
				String name = sweElement.getName();//alloHalloNameHallo
				// </r529>
				TextDrawer.text(p, new Vector(v111.getX(), v111.getY(), v111.getZ()), name, RuntimeParams.COLOR__WHITE, 36);
				float nameWidth = p.textWidth(name);
				changeLocalDimensions(new Dimensions(nameWidth/80, v111.getY(), v111.getZ()));
//				TextDrawer.text(p,
//						new Vector(v111.getX(), v111.getY() * 20, v111.getZ()),
//						name, RuntimeParams.COLOR__WHITE, 45);
//				float nameWidth = p.textWidth(name);
//				changeLocalDimensions(new Dimensions(nameWidth / 31, v111.getY(),
//						v111.getZ()));

				
			}
			// </r522>
		}
		p.popMatrix();
		p.popStyle();
	}

	// @Override
	// protected void draw(PApplet p) {
	// super.draw(p);
	// }

	@Override
	public Vector getLocalPosition(Vector vector) {
		return super.getLocalPosition(vector);
	}

	// <>
	// protected GFX_Node getNode() {
	// return (GFX_Node)navi_getParentObject();
	// }
	// </>

	@Override
	public void log() {
		LogManager.logALL("<GFX_Label>");
		LogManager.indent();
		{
			super.log();
		}
		LogManager.deindent();
	}

	@Override
	public String toString() {
		return "<GFX_Label " + labelId + ">";
	}

}
